package cn.itsource.ymcc.consumer;

import cn.itsource.ymcc.constant.Constants;
import cn.itsource.ymcc.constant.OrderConstants;
import cn.itsource.ymcc.constant.PayConstants;
import cn.itsource.ymcc.constant.RConst;
import cn.itsource.ymcc.domain.CourseOrder;
import cn.itsource.ymcc.mapper.CourseOrderMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@Slf4j
@Component
@RocketMQMessageListener(
        consumerGroup = "CourseOrderConsumer",//消费者组名
        topic = OrderConstants.TOPIC_ORDER_MESSAGE_DELAY,
        selectorExpression = OrderConstants.TAGS_ORDER_MESSAGE_DELAY
)
public class CourseOrderConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private CourseOrderMapper courseOrderMapper;

    /**
     * 延迟消息  修改订单状态  用户未支付
     * @param messageExt
     */
    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            byte[] body = messageExt.getBody();
            String orderSn = new String(body, Constants.UTF8);
            CourseOrder courseOrder = new CourseOrder();
            courseOrder.setStatusOrder(RConst.Num.INT_4);
            courseOrderMapper.update(courseOrder,new QueryWrapper<CourseOrder>().eq("order_no",orderSn)
                    .eq("status_order",RConst.Num.BASE_LONG_0));
            //去关闭支付宝的支付接口
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
